﻿// -----------------------------------------------------------------------
// <copyright file="EBenefitsMembershipProvider.cs" company="Ong Zhi Xian">
// Ong Zhi Xian
// </copyright>
// -----------------------------------------------------------------------

namespace eBenefits.App.Providers
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Text;
    using System.Web.Security;
    using eBenefits.App.Helpers;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class eBenefitsMembershipProvider : MembershipProvider
    {
        #region MembershipProvider members

        #region public properties

        public override string ApplicationName
        {
            get;
            set;
        }
        
        private bool enablePasswordReset = false;
        public override bool EnablePasswordReset
        {
            get { return this.enablePasswordReset; }
        }

        private bool enablePasswordRetrieval = false;
        public override bool EnablePasswordRetrieval
        {
            get { return this.enablePasswordRetrieval; }
        }

        private bool requiresQuestionAndAnswer = false;
        public override int MaxInvalidPasswordAttempts
        {
            get { return this.maxInvalidPasswordAttempts; }
        }

        private bool requiresUniqueEmail = false;
        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return this.minRequiredNonAlphanumericCharacters; }
        }

        private int maxInvalidPasswordAttempts = 3;
        public override int MinRequiredPasswordLength
        {
            get { return this.minRequiredPasswordLength; }
        }

        private int minRequiredNonAlphanumericCharacters = 0;
        public override int PasswordAttemptWindow
        {
            get { return this.passwordAttemptWindow; }
        }

        private int minRequiredPasswordLength = 6;
        public override MembershipPasswordFormat PasswordFormat
        {
            get { return this.passwordFormat; }
        }

        private int passwordAttemptWindow = 3;
        public override string PasswordStrengthRegularExpression
        {
            get { return this.passwordStrengthRegularExpression; }
        }

        private MembershipPasswordFormat passwordFormat = MembershipPasswordFormat.Hashed;
        public override bool RequiresQuestionAndAnswer
        {
            get { return this.requiresQuestionAndAnswer; }
        }

        private string passwordStrengthRegularExpression = string.Empty;
        public override bool RequiresUniqueEmail
        {
            get { return this.requiresUniqueEmail; }
        }

        #endregion public properties

        #region password related functions
        
        public override string GetPassword(string username, string answer)
        {
            return string.Empty;
        }

        public override string ResetPassword(string username, string answer)
        {
            //eBenefits.Model.Entities.
            //eBenefits.DataEntities.LoginAccount
            //x.LoginAccounts.AddObject()
            
            string newPassword = Membership.GeneratePassword(this.MinRequiredPasswordLength, this.MinRequiredNonAlphanumericCharacters);
            throw new NotImplementedException();
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            return true;
        }

        #endregion password related functions

        #region membership enquiry related functions
        
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        #endregion membership enquiry related functions

        #region user related functions

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }
        
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        #endregion user related functions

        #endregion MembershipProvider members

        public string ConnectionStringName
        {
            get;
            private set;
        }

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
            this.ConnectionStringName = DataHelper.GetString(config["ConnectionStringName"], "DefaultConnection");

            this.enablePasswordReset = DataHelper.GetBool(config["enablePasswordReset"], true);
            this.enablePasswordRetrieval = DataHelper.GetBool(config["enablePasswordRetrieval"], false);
            this.requiresQuestionAndAnswer = DataHelper.GetBool(config["requiresQuestionAndAnswer"], false);
            this.requiresUniqueEmail = DataHelper.GetBool(config["requiresUniqueEmail"], false);

            this.minRequiredPasswordLength = DataHelper.GetInt(config["minRequiredPasswordLength"], 6);
            this.minRequiredNonAlphanumericCharacters = DataHelper.GetInt(config["minRequiredNonAlphanumericCharacters"], 0);

            this.passwordFormat = DataHelper.GetPasswordFormat(config["passwordFormat"], MembershipPasswordFormat.Hashed);
            this.maxInvalidPasswordAttempts = DataHelper.GetInt(config["maxInvalidPasswordAttempts"], 6);
            this.passwordAttemptWindow = DataHelper.GetInt(config["passwordAttemptWindow"], 6);
            this.passwordStrengthRegularExpression = DataHelper.GetString(config["passwordStrengthRegularExpression"], string.Empty);
        }
    }
}
